﻿
#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
#endregion

public partial class ucActorPermissionAssign : System.Web.UI.UserControl
{
    #region Fileds
    UserManager _userPermission = new UserManager();
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSave.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSave.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");


        if(!IsPostBack)
            InitialiseDDLs();
    }    

    protected void btnSave_Click(object sender, EventArgs e)
    {        

        if (ddlPermission.SelectedIndex == 0)
        {
            Response.Write("<script>alert('Select Permission')</script>");
            return;
        }

        if (ddlUtility.SelectedIndex == 0)
        {
            Response.Write("<script>alert('Select Utility')</script>");
            return;
        }

        Save();
        //Clear();
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucActorPermissionSearch.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    #endregion

    #region Methods
    void InitialiseDDLs()
    {
        DataSet dsPermission ;                   
        txtUser_Role.Text =  Session["UserManager_User/RoleName"].ToString();                          
               
        dsPermission = _userPermission.GetData("UtilityList", string.Empty);
        ddlUtility.DataSource = dsPermission;
        ddlUtility.DataTextField = "UT_UtilityName";
        ddlUtility.DataValueField = "UT_UtilityID";
        ddlUtility.DataBind();
        ddlUtility.Items.Insert(0, "Select");

        dsPermission.Clear();
        dsPermission = _userPermission.GetData("PermissionList", string.Empty);
        ddlPermission.DataSource = dsPermission;
        ddlPermission.DataTextField = "PL_PermissionName";
        ddlPermission.DataValueField = "PL_PermissionID";
        ddlPermission.DataBind();
        ddlPermission.Items.Insert(0, "Select");
       

    }

    void Save()
    {

        try
        {

            UserPermission permission = new UserPermission();

            permission.ActorType = Session["UserManager_Type"].ToString();
            permission.ActorID = Session["UserManager_User/RoleID"].ToString();
            permission.PermissionID = ddlPermission.SelectedValue;
            permission.UtilityID = ddlUtility.SelectedValue;

            if (!_userPermission.AssignPermission(permission))
            {
                Response.Write("<script>alert('Already Exist')</script>");
            }
            else
                Response.Write("<script>alert('Assigned Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Assign Permission Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Clear()
    {
        txtUser_Role.Text = string.Empty;
        ddlPermission.SelectedIndex = 0;

    }

    #endregion

}
 